package com.example.musicdemo.presenter.impl

import com.example.musicdemo.base.BaseListPresenter
import com.example.musicdemo.base.BaseView
import com.example.musicdemo.model.Music
import com.example.musicdemo.net.ResponseHandler
import com.example.musicdemo.net.SearchRequest
import com.example.musicdemo.presenter.interf.SearchPresenter

class SearchPresenterImp(private var searchView: BaseView<List<Music>>?) : SearchPresenter,
    ResponseHandler<List<Music>> {

    companion object {
        var sSearchTing: String? = null
    }

    override fun setSearchThing(searchThing: String) {
        sSearchTing = searchThing
    }

    override fun loadData() {
        if (sSearchTing != null) {
            SearchRequest(BaseListPresenter.TYPE_INIT_OR_REFRESH, 0, sSearchTing!!, this).excute()
        }
    }

    override fun loadMore(offset: Int) {
        sSearchTing?.let {
            SearchRequest(
                BaseListPresenter.TYPE_LOAD_MORE, offset,
                sSearchTing!!, this
            ).excute()
        }
    }

    override fun destroyView() {
        if (searchView != null) {
            searchView = null
        }
    }

    override fun onError(type: Int, msg: String) {
        searchView?.onError(msg)
    }

    override fun onSuccess(type: Int, result: List<Music>) {
        when (type) {
            BaseListPresenter.TYPE_INIT_OR_REFRESH -> searchView?.loadSuccess(result)
            BaseListPresenter.TYPE_LOAD_MORE -> {
                searchView?.loadMore(result)
            }
        }
    }


}